package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
* 角色服务
* */
@Service(interfaceClass = RoleManagementService.class)
@Transactional
public class RoleManagementServiceImpl implements RoleManagementService {
    @Autowired
    private RoleDao roleDao;
    //新增角色，同时让角色关联权限
    public void add(Role role, Integer[] permissionIds) {
     //新增角色，操作t_role表
      roleDao.add(role);
      //关联t_permission
        Integer roleId = role.getId();
        if(permissionIds !=null && permissionIds.length > 0){
            for (Integer permissionId : permissionIds) {
                Map<String,Integer> map = new HashMap<>();
                map.put("permissionId",permissionId);
                map.put("roleId",roleId);
                roleDao.setRoleAndPermission(map);
            }
        }
    }

    //分页查询
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);
        Page<Role>page=roleDao.findByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    //根据id查询角色
    public Role findById(Integer id) {
        return roleDao.findById(id);
    }

    //根据角色ID查询关联权限ID
    public List<Integer> findPermissionIdsByRoleId(Integer id) {
        return roleDao.findPermissionIdsByRoleId(id);
    }

    //编辑角色信息，关联权限信息
    public void edit(Role role, Integer[] permissionIds) {
    //修改角色信息，操作t_role表
        roleDao.edit(role);
        //清理当前角色关联的权限表，操作t_role_permission
        roleDao.deleteAssociation(role.getId());
        //重新建立关联关系
        Integer roleId = role.getId();
        if(permissionIds !=null && permissionIds.length > 0){
            for (Integer permissionId : permissionIds) {
                Map<String,Integer> map = new HashMap<>();
                map.put("permissionId",permissionId);
                map.put("roleId",roleId);
                roleDao.setRoleAndPermission(map);
            }
        }
    }

    //根据id删除角色，先删除中间表中的id
    public void delete(Integer id) {
        roleDao.deleteAssociation(id);
        roleDao.deleteById(id);

    }

    //查询所有角色信息
    public List<Role> findAll() {
        return roleDao.findAll();
    }

    //建立角色和权限多对多关系
    public void setRolrAndPermission(Integer roleId,Integer[] permissionIds){
        //设置多对多关系
        if(permissionIds != null && permissionIds.length > 0){
            for (Integer permissionId : permissionIds) {//检查项id
                Map<String,Integer> map = new HashMap<>();
                map.put("checkgroupId",roleId);
                map.put("checkitemId",permissionId);
                roleDao.setRoleAndPermission(map);
            }
        }
    }
}
